// const { LinValidator, Rule } = require('../../core/lin-validator-v2')
const { routerValidator, Rule } = require('../../core/router-validator')
const { LoginType, ArtType } = require('../lib/enum')
const { User } = require('../models/user')

class PositiveIntegerValidator extends routerValidator {
    constructor() {
        super()
        this.id = [
            new Rule('isInt', '需要是正整数', {
                min: 1
            }),
        ]
    }
}

// 验证 注册
class RegisterValidator extends routerValidator {
    constructor() {
        super()
        this.email = [
            new Rule('isEmail', '不符合Email规范')
        ]
        this.password1 = [
            // 用户指定范围 123456 $^
            new Rule('isLength', '密码至少6个字符，最多32个字符', {
                min: 6,
                max: 32
            }),
            new Rule('matches', '密码不符合规范', '^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]')

        ]
        this.password2 = this.password1
        this.nickname = [
            new Rule('isLength', '昵称不符合长度规范', {
                min: 4,
                max: 32
            }),
        ]
    }
    validatePassword(vals) {
        const psw1 = vals.body.password1
        const psw2 = vals.body.password2
        if (psw1 !== psw2) {
            throw new Error('两个密码必须相同')
        }
    }

    // 验证邮箱是否重复
    async validateEmail(vals) {
        const email = vals.body.email;
        const user = await User.findOne({
            where: {
                email: email
            }
        })

        if (user) {
            throw new Error('email 已存在')
        }
    }
}

// token 验证
class TokenValidator extends routerValidator {
    constructor() {
        super()
        // 账号验证
        this.account = [
            new Rule('isLength', '你的账号不符合规则', {
                min: 4,
                max: 32
            })
        ]

        // 密码验证  小程序登录 不需要密码
        this.scret = [
            new Rule('isOptional'), // 是否需要密码
            // 如果需要密码验证  密码
            new Rule('isLength', '您输入的密码不符合规则', {
                min: 6,
                max: 128
            }),
            new Rule('matches', '密码不符合规范', '^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]')
        ]
    }
    // 登录方式
    validateLoginTytp(vals) {
        const type = parseInt(vals.body.type)
        if (!vals.body.type) {
            throw new Error('type:登录方式，未传')
        }

        if (!LoginType.isThisType(type)) {
            throw new Error('type:登录方式不存在')
        }
    }
}

// toke 是否过期
class NotEmptyValidator extends routerValidator {
    constructor() {
        super()
        this.token = [
            new Rule('isLength', '参数能为空', {
                min: 1
            })
        ]
    }
}
function _checkType(vals) {
    let type = vals.body.type || vals.path.type;
    if (!type) {
        throw new Error('type:登录方式，未传')
    }
    type = parseInt(type)
    if (!ArtType.isThisType(type)) {
        throw new Error('type:登录方式不存在')
    }
}
class LikeValidator extends PositiveIntegerValidator {
    constructor() {
        super()
        this.validateType = _checkType
    }
}
// 验证 type id 
class ClassicValidator extends LikeValidator {
}

// 验证 查询参数  有分页
class SearchValidator extends routerValidator {
    constructor() {
        super()
        this.q = [
            new Rule('isLength', '搜索关键字不能为空', {
                min: 1,
                max: 32
            })
        ]
        // 从 那开始
        this.start = [
            new Rule('isInt', 'start 不符合规范', {
                min: 0,
                max: 60000
            }),
            new Rule('isOptional', '', 0)
        ]
        // 返回多少条
        this.count = [
            new Rule('isInt', 'count 不符合规范', {
                min: 1,
                max: 20
            }),
            new Rule('isOptional', '', 20)

        ]
    }
}

// 新增书籍短评  校验评论长度
class AddShortCommentValidator extends routerValidator{
    constructor(){
        super()
        this.content =[
            new Rule('isLength','必须是1到24个字符',{
                min:1,
                max:24
            })
        ]
    }
}

module.exports = {
    PositiveIntegerValidator,
    RegisterValidator,
    TokenValidator,
    NotEmptyValidator,
    LikeValidator,
    ClassicValidator,
    SearchValidator,
    AddShortCommentValidator
}